﻿#region LGPL License

/*
OpenSearchNet
Copyright (c) 2014, Brad Hards <bradh@frogmouth.net>, All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library.
*/

#endregion LGPL License

namespace OpenSearch
{
    /// <summary>
    /// Content associated with a ResponseItem
    /// </summary>
    public class ResponseItemContent
    {
        private string mimeType;
        private string body;

        /// <summary>
        /// Construct from a SyndicationContent instance
        /// </summary>
        /// <param name="syndicationContent">the SyndicationContent providing the content</param>
        internal ResponseItemContent(System.ServiceModel.Syndication.SyndicationContent syndicationContent)
        {
            mimeType = syndicationContent.Type;
            if (mimeType.Equals("text") || mimeType.ToUpperInvariant().Contains("HTML"))
            {
                body = (syndicationContent as System.ServiceModel.Syndication.TextSyndicationContent).Text;
            }
            else if (mimeType.ToUpperInvariant().Contains("XML"))
            {
                System.ServiceModel.Syndication.XmlSyndicationContent sc = syndicationContent as System.ServiceModel.Syndication.XmlSyndicationContent;
                System.Xml.XmlDictionaryReader reader = sc.GetReaderAtContent();
                body = reader.ReadOuterXml();
            }
            else
            {
                body = "";
            }
        }

        internal ResponseItemContent()
        {
            mimeType = "";
            body = "";
        }

        /// <summary>
        /// The MIME type of the content
        /// </summary>
        /// <remarks>
        /// <para>
        /// This can be "text", "html", "xhtml" or an MIME media type
        /// </para>
        /// </remarks>
        public string MimeType
        {
            get
            {
                return mimeType;
            }
        }

        /// <summary>
        /// Body of the item
        /// </summary>
        /// <remarks>
        /// <para>
        /// OpenSearch responses are fairly flexible in what can be included in the response. This can be plain text,
        /// HTML, binary images, or another format such as Atom.
        /// </para>
        /// </remarks>
        public string Body
        {
            get
            {
                return body;
            }
        }
    }
}